<template>
  <div class="app-container">
    <div class="filter-container">
      <el-select v-model="listQuery.daili" placeholder="请选择代理账号" clearable style="width: 150px" filterable class="filter-item" @change="handleFilter">
        <el-option v-for="(value) in dailiArr" :key="value" :label="value" :value="value" />
      </el-select>
      <el-date-picker
        v-model="value1"
        align="left"
        unlink-panels
        type="daterange"
        range-separator="至"
        start-placeholder="开始日期"
        end-placeholder="结束日期"
        class="filter-item pickertime"
        format="yyyy-MM-dd"
        value-format="yyyyMMdd"
        :picker-options="pickerOptions2"
        @change="handleFilter"
      />
      <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">
        搜索
      </el-button>
      <el-button v-waves class="filter-item" type="primary" icon="el-icon-refresh" @click="handleCancel">
        重置
      </el-button>
      <span style="margin-left: 20px" />
      <template>
        <el-radio-group v-model="listQuery.status" @change="handleFilter">
          <el-radio label="">全部</el-radio>
          <el-radio :label="1">已结算</el-radio>
          <el-radio :label="0">未结算</el-radio>
          <el-radio :label="2">已作废</el-radio>
        </el-radio-group>
      </template>
    </div>

    <el-table
      :key="tableKey"
      v-loading="listLoading"
      :data="list"
      border
      fit
      :summary-method="getSummaries"
      show-summary
      highlight-current-row
      style="width: 100%;"
    >
      <el-table-column label="日期" align="center" min-width="100">
        <template slot-scope="{row}">
          <span>{{ row.day }}</span>
        </template>
      </el-table-column>
      <el-table-column label="代理账号" min-width="100" align="center">
        <template slot-scope="{row}">
          <span>{{ row.qudao }}</span>
        </template>
      </el-table-column>
      <el-table-column label="业务周期" min-width="240" align="center">
        <template slot-scope="{row}">
          <span>{{ row.cycle }}</span>
        </template>
      </el-table-column>
      <el-table-column label="启动UV" min-width="100" align="center">
        <template slot-scope="{row}">
          <span>{{ row.uv }}</span>
        </template>
      </el-table-column>
      <el-table-column label="启动PV" min-width="100" align="center">
        <template slot-scope="{row}">
          <span>{{ row.pv }}</span>
        </template>
      </el-table-column>
      <el-table-column label="CNZZ_IP" min-width="100" align="center">
        <template slot-scope="{row}">
          <span>{{ row.ip }}</span>
        </template>
      </el-table-column>
      <el-table-column label="万次IP单价" min-width="100" align="center">
        <template slot-scope="{row}">
          <span>{{ row.price }}</span>
        </template>
      </el-table-column>
      <el-table-column label="支付金额" min-width="100" align="center">
        <template slot-scope="{row}">
          <span>{{ row.money }}</span>
        </template>
      </el-table-column>
      <el-table-column label="状态" min-width="80" align="center">
        <template slot-scope="{row}">
          <el-tag :type="row.status | statusFilter">
            {{ allStatus[row.status] }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" min-width="180" class-name="small-padding fixed-width">
        <template slot-scope="{row}">
          <el-button :type="row.status!=0? 'info':'primary'" :disabled="row.status!=0" size="mini" @click="handleSettle(row)">
            结算
          </el-button>
          <el-button size="mini" :type="row.status!=0? 'info':'danger'" :disabled="row.status!=0" @click="handleDel(row)">
            作废
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" class="pageShow" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList" />
    <pagination v-show="total > 0" layout="prev, pager, next" :small="true" class="pageHidden" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList" />
  </div>
</template>

<script>
import { fetchList, fetchDaili, settleMoney } from '@/api/cnzz/settle'
import waves from '@/directive/waves' // waves directive
import Pagination from '@/components/Pagination' // secondary package based on el-pagination
import { MessageBox, Message } from 'element-ui'

export default {
  name: 'Settle1',
  components: { Pagination },
  directives: { waves },
  filters: {
    statusFilter(status) {
      const statusMap = {
        1: 'success',
        0: 'danger',
        2: 'info'
      }
      return statusMap[status]
    }
  },
  data() {
    return {
      tableKey: 0,
      list: null,
      total: 0,
      listLoading: true,
      value1: '',
      countPv: '',
      countUv: '',
      countIp: '',
      countCnzzUv: '',
      countProfit: '',
      allStatus: {
        0: '未结算',
        1: '已结算',
        2: '作废'
      },
      pickerOptions2: {
        shortcuts: [{
          text: '最近一周',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 7)
            picker.$emit('pick', [start, end])
          }
        }, {
          text: '最近一月',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 30)
            picker.$emit('pick', [start, end])
          }
        }]
      },
      listQuery: {
        page: 1,
        limit: 20,
        daili: '',
        status: '',
        start_tm: '',
        end_tm: ''
      },
      dailiArr: []
    }
  },
  created() {
    this.getList()
    this.getDaili()
  },
  methods: {
    getList() {
      this.listLoading = true
      if (this.value1) {
        this.listQuery.start_tm = this.value1[0]
        this.listQuery.end_tm = this.value1[1]
      } else {
        this.listQuery.start_tm = ''
        this.listQuery.end_tm = ''
      }
      fetchList(this.listQuery).then(response => {
        this.list = response.data
        this.total = response.total
        this.countPv = response.count_pv
        this.countUv = response.count_uv
        this.countIp = response.count_ip
        this.countCnzzUv = response.count_cnzz_uv
        this.countProfit = response.count_money
        this.listLoading = false
      })
    },
    getDaili() {
      fetchDaili().then(response => {
        this.dailiArr = response.data
      })
    },
    getSummaries() {
      const sums = ['总计', '-', '-', this.countUv, this.countPv, this.countIp, '-', this.countProfit, '-', '-']
      return sums
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    handleCancel() {
      this.value1 = ''
      this.listQuery.page = 1
      this.listQuery.daili = ''
      this.listQuery.status = ''
      this.getList()
    },
    handleSettle(row) {
      MessageBox.confirm('确定结算吗？', '确认', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        const data = {
          id: row.id,
          status: 1
        }
        settleMoney(data).then(response => {
          if (response.code === 1) {
            this.getList()
            Message({
              message: response.msg || '结算成功',
              type: 'success',
              duration: 2000
            })
          } else {
            Message({
              message: response.msg || '结算失败',
              type: 'error',
              duration: 2000
            })
          }
        })
      }).catch(() => {})
    },
    handleDel(row) {
      MessageBox.confirm('确定要作废吗？', '确认', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        const data = {
          id: row.id,
          status: 2
        }
        settleMoney(data).then(response => {
          if (response.code === 1) {
            this.getList()
            Message({
              message: response.msg || '作废成功',
              type: 'success',
              duration: 2000
            })
          } else {
            Message({
              message: response.msg || '作废失败',
              type: 'error',
              duration: 2000
            })
          }
        })
      }).catch(() => {})
    }
  }
}
</script>

<style scoped>
  .pageHidden{
    display: none;
  }
  @media (max-width:550px) {
    .pageShow {
      display: none;
    }
    .pageHidden {
      display: block;
    }
    .pickertime{
      width: 100%;
    }
  }
</style>
